//
//  SABaseNavigationController.m
//  SunOn
//
//  Created by RazeenCheng on 2017/5/4.
//  Copyright © 2017年 NeoZhang. All rights reserved.
//

#import "SABaseNavigationController.h"

@interface SABaseNavigationController ()<UINavigationControllerDelegate,UIGestureRecognizerDelegate>

@end

@implementation SABaseNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.delegate = self;
    
    // 设置标题的字体大小和颜色
    [self.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"0D88E8"]}];
    
    // 屏幕边缘滑动(只能在屏幕的边缘才能触发该手势，不能在屏幕的任意一点触发该手势)
    UIScreenEdgePanGestureRecognizer *edgePanGestureRecognizer = (UIScreenEdgePanGestureRecognizer *)self.interactivePopGestureRecognizer;
    
    // 滑动手势（禁用系统自带的屏幕边缘滑动手势，使用自定义的滑动手势目的就是达到触摸屏幕上的任意一点向右滑动都能实现返回的效果）
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:edgePanGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
    panGestureRecognizer.delegate = self;
    [self.view addGestureRecognizer:panGestureRecognizer];
    
    // 禁用系统的屏幕边缘滑动手势
    edgePanGestureRecognizer.enabled = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


// 是否允许触发手势，如果是根视图控制器则不需要
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    return self.childViewControllers.count > 1;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 统一设置返回按钮
    NSInteger count = self.childViewControllers.count;
    if (count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"tb_fh"] style:(UIBarButtonItemStylePlain) target:self action:@selector(back)];
    }
    
    [super pushViewController:viewController animated:animated];
}

- (void)back {
    [self popViewControllerAnimated:YES];
}

- (void)handleNavigationTransition:(UIPanGestureRecognizer *)panGestureRecognizer
{
    
}

@end
